﻿#include "datadisplaywidget.h"
#include <QPainter>
#include <QBrush>
#include <QString>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QSplitter>
#include <QDebug>

//构造函数
CDataWidget::CDataWidget(QWidget *parent) : QWidget(parent)
{
    InitFace();
    InitData();
}

//析构函数
CDataWidget::~CDataWidget()
{

}

//界面初始化函数
void CDataWidget::InitFace()
{
    //外框架 0
    QVBoxLayout *vblayout0 = new QVBoxLayout();
        //框架1
        QVBoxLayout *vblayout1 = new QVBoxLayout();
            //移动垂直分栏1_1
            QSplitter *spliter1_1  = new QSplitter(Qt::Vertical);
            //框架1.1
            QWidget *wgt1_1  = new QWidget();
            QHBoxLayout *hblayout1_1 = new QHBoxLayout();
                //框架1.1.1
                QHBoxLayout *vhl1_1_1 = new QHBoxLayout();
                    //移动水平分栏 1.1.1
                    QSplitter *spliter1_1_1  = new QSplitter(Qt::Horizontal);
                    textTag1=new QTextEdit();
                    textCmd1=new QTextEdit();
                    spliter1_1_1->addWidget(textTag1);
                    spliter1_1_1->addWidget(textCmd1);
                    //spliter1_1_1->setStretchFactor(0,1);
                    //spliter1_1_1->setStretchFactor(1,5);
                    textTag1->setFixedWidth(100);
                    textTag1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                    textCmd1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                vhl1_1_1->addWidget(spliter1_1_1);
                //框架1.1.2
                scrollBar1 = new QScrollBar();
                scrollBar1->setMinimum(0);
            hblayout1_1->addLayout(vhl1_1_1);
            hblayout1_1->addWidget(scrollBar1);
            hblayout1_1->setMargin(0);
            wgt1_1->setLayout(hblayout1_1);
            //框架1.2
            QWidget *wgt1_2  = new QWidget();
            QHBoxLayout *hblayout1_2 = new QHBoxLayout();
                //框架1.2.1
                QHBoxLayout *vhl1_2_1 = new QHBoxLayout();
                    //移动分栏框架 1.2.1
                    QSplitter *spliter1_2_1  = new QSplitter(Qt::Horizontal);
                    textTag2=new QTextEdit();
                    textCmd2=new QTextEdit();
                    spliter1_2_1->addWidget(textTag2);
                    spliter1_2_1->addWidget(textCmd2);
                    //spliter1_2_1->setStretchFactor(0,1);
                    //spliter1_2_1->setStretchFactor(1,5);
                    textTag2->setFixedWidth(100);
                    textTag2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                    textCmd2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                vhl1_2_1->addWidget(spliter1_2_1);
                //框架1.2.2
                scrollBar2 = new QScrollBar();
                scrollBar2->setMinimum(0);
            hblayout1_2->addLayout(vhl1_2_1);
            hblayout1_2->addWidget(scrollBar2);
            hblayout1_2->setMargin(0);
            wgt1_2->setLayout(hblayout1_2);

            //移动垂直分栏1_1
            spliter1_1->addWidget(wgt1_2);
            spliter1_1->addWidget(wgt1_1);
            spliter1_1->setStretchFactor(0,10);
            spliter1_1->setStretchFactor(1,10);

        //框架1
        vblayout1->addWidget(spliter1_1);

        //框架 2
        label1 = new QLabel("                   项目1     项目2     项目3      项目4      项目5      项目6");
        //框架 3
        label2 = new QLabel("--");
        vblayout0->addWidget(label1);
    vblayout0->addLayout(vblayout1);
    vblayout0->addWidget(label2);
    vblayout0->setStretchFactor(label1,1);
    vblayout0->setStretchFactor(vblayout1,20);
    vblayout0->setStretchFactor(label2,1);
    vblayout0->setMargin(0);

    setLayout(vblayout0);

    /*
    //外框架 0
    QVBoxLayout *vblayout = new QVBoxLayout();
        //框架 2
        QHBoxLayout *hblayout = new QHBoxLayout();
            //框架1.1
            QHBoxLayout *vhl1_1 = new QHBoxLayout();
                //移动分栏框架 1.1.1
                QSplitter *spliter  = new QSplitter();
                textTag1=new QTextEdit();
                textCmd1=new QTextEdit();
                spliter->addWidget(textTag1);
                spliter->addWidget(textCmd1);
                spliter->setStretchFactor(0,1);
                spliter->setStretchFactor(1,5);
                textTag1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                textCmd1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            vhl1_1->addWidget(spliter);
            //框架1.2
            scrollBar1 = new QScrollBar();
            scrollBar1->setMinimum(0);
        hblayout->addLayout(vhl1_1);
        hblayout->addWidget(scrollBar1);
        hblayout->setMargin(0);
        //框架 3
        label2 = new QLabel("--");
    vblayout->addLayout(hblayout);
    vblayout->addWidget(label2);
    vblayout->setStretchFactor(hblayout,20);
    vblayout->setStretchFactor(label2,1);
    vblayout->setMargin(0);

    setLayout(vblayout);*/

    connect(scrollBar1,SIGNAL(valueChanged(int)),this,SLOT(OnScrollBar1(int)));
    connect(this,SIGNAL(updateview(int)),this,SLOT(UpdateView(int)));
}

//数据初始化函数
void CDataWidget::InitData()
{
    cmdInfo.push_back("AA CC");
    cmdColor.push_back(Qt::blue);
    cmdInfo.push_back("CC AA");
    cmdColor.push_back(Qt::red);
}

//添加数据函数
void CDataWidget::AddTag()
{
    //根据listShow数据添加附加信息
//    QByteArray arr;
//    char ch;
//    int tt;
//    foreach (arr, *listShow)
//    {
//        //ch = arr.at(arr.count()-1);
//        tt=listShow->indexOf(arr);
//        listTag.push_back(QString::number(tt,10));
//    }
    for(int i=1;i<=listShow->count();i++)
    {
        listTag.push_back(QString::number(scrollBar1->value()-maxRow+i,10));
    }
}

//paintEvent自动刷新
void CDataWidget::paintEvent(QPaintEvent *e)
{
    //界面可显示行数计算
    maxRow = textCmd1->height()
            /(int)(textCmd1->fontInfo().pixelSize() * 1.2);
    if(count>maxRow)
        scrollBar1->setMinimum(maxRow);
//    qDebug()<<" UpdateView()\r\n";
}
//界面手动刷新函数
void CDataWidget::UpdateView()
{
    count = cmdStorage->Count();
    scrollBar1->setMaximum(count);

    //界面可显示行数计算
    maxRow = textCmd1->height()
            /(int)(textCmd1->fontInfo().pixelSize() * 1.2);
    if(count>maxRow)
        scrollBar1->setMinimum(maxRow);

    scrollBar1->setValue(count);
}

//滚动条刷新函数
void CDataWidget::OnScrollBar1(int pos)
{
    //其他信息显示
    QString str1 = QString::number(scrollBar1->minimum(),10);
    QString str2 = QString::number(scrollBar1->value(),10);
    QString str3 = QString::number(scrollBar1->maximum(),10);
    label2->setText("min:"+str1+"  "+"now:"+str2+"  "+"max:"+str3);

    //设置最少显示行数为row行或小于row的实际行数
    int row = pos<maxRow?pos:maxRow;
    //从dataStorage的获取显示数据
    listShow = cmdStorage->GetListData(pos-row,row);
    if(listShow->isEmpty())return;
    //添加附加信息
    AddTag();
    //将显示数据转换为字符串以显示
    QString strCmd,strTag;
    for(qint32 i=0;i<row;i++)
    {
        //常规算法
        //十六进制转字符串
        QString str1 = listShow->at(i).toHex().toUpper();
        //插入空格
        QString str2;
       for(qint32 index=0;index<str1.count();index++)
       {
           if(index>0&&index%2==0)
               str2.push_back(" ");
           str2.push_back(str1[index]);
       }
       strCmd.append(str2+"\r\n");

       if(!listTag.isEmpty()) strTag.append(listTag.at(i)+"\r\n");
    }
    //textTag2
    textTag1->clear();
    textTag1->setText(strTag);
    textTag1->setFont(QFont("Fantasy",12,QFont::Decorative));
    //刷新textCmd2
    textCmd1->clear();
    textCmd1->setText(strCmd);
    textCmd1->setFont(QFont("Fantasy",12,QFont::Decorative));
    //设置文字默认显示颜色为绿色
    //textCmd2->setTextColor(Qt::blue);

    //根据cmdInfo和cmdColor设置特定信息文字的颜色，如区分收发数据
    QString sInfo;
    foreach (sInfo,cmdInfo)
    {
        QString search_text=sInfo;
        if (search_text.trimmed().isEmpty())return;
        else
        {
            QTextDocument *document = textCmd1->document();
            bool found = false;
            QTextCursor highlight_cursor(document);
            QTextCursor cursor(document);

            //开始
            cursor.beginEditBlock();
            QTextCharFormat color_format(highlight_cursor.charFormat());
            color_format.setForeground(cmdColor.at(cmdInfo.indexOf(sInfo)) );
            while (!highlight_cursor.isNull() && !highlight_cursor.atEnd())
            {
                //查找指定的文本，匹配整个单词
                highlight_cursor = document->find(search_text, highlight_cursor, QTextDocument::FindWholeWords);
                if (!highlight_cursor.isNull())
                {
                    if(!found)found = true;
                    highlight_cursor.select(QTextCursor::BlockUnderCursor); //选中光标所在block的内容
                    //edit->setTextCursor(highlight_cursor);
                    highlight_cursor.mergeCharFormat(color_format);
                }
            }
            cursor.endEditBlock();
            //结束
            if (found==false)return;
        }
    }

    //清理数据
    if(!listShow->isEmpty()) listShow->clear();
    if(!listTag.isEmpty()) listTag.clear();
}
